<template>
    <div class="container">
        <SessionTable :data="tableData" />
    </div>
</template>

<script lang="ts" setup>
import { onActivated } from 'vue';


import { ref } from 'vue';
import SessionTable from '@/components/session-table.vue';
import { fetchStudentCourse, fetchSessionByTcId ,fetchTeachClassPage} from '@/api/index';
import { ta } from 'element-plus/es/locale';

const tableData = ref([
]);

const getData = async () => {
    if(localStorage.getItem('vuems_role')=='student'){
        let scres = await fetchStudentCourse({ index: 1, size: 1000000 ,sid:localStorage.getItem('sid')});
    let scList = scres.data.pageList;
    scList.forEach(async (scItem) => {
        const sessionRes = await fetchSessionByTcId(scItem.teachClass.tc_id)
        const sessionList = sessionRes.data;
        sessionList.forEach(session => {
            let day = session.weekday;
            let start = session.start_time-1;
            let end = session.end_time-1;
            for (let i = start; i <= end; i++) {
                tableData.value[i][day] = scItem.course.name;
            }
        })
        console.log(tableData.value)
    })
    }

    else{
        let tcres = await fetchTeachClassPage({ index: 1, size: 10000000 });
    let tcList = tcres.data.pageList;
    tcList.forEach(async (tcItem) => {
        const sessionRes = await fetchSessionByTcId(tcItem.tc_id)
        const sessionList = sessionRes.data;
        sessionList.forEach(session => {
            let day = session.weekday;
            let start = session.start_time-1;
            let end = session.end_time-1;
            for (let i = start; i <= end; i++) {
                tableData.value[i][day] = tcItem.course.name;
            }
        })
        console.log(tableData.value)
    })
    }

}
tableData.value = []
for (let i = 0; i < 10; i++) {
    let day = ['第' + (i + 1) + '节']
    for (let j = 1; j <= 7; j++) {
        day.push('')
    }
    tableData.value.push(day)
}
onActivated(() => {
    getData();
});

</script>